home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / TUT01NEW.ZIP / TUT1.CPP < prev    next >
C/C++ Source or Header  |  1994-10-21  |  7KB  |  162 lines

  1. /*****************************************************************************/
  2. /*                                                                           */
  3. /* TUTPROG1.CPP - VGA Trainer Program 1 (in Turbo C++ 3.0)                   */
  4. /*                                                                           */
  5. /* "The VGA Trainer Program" is written by Denthor of Asphyxia.  However it  */
  6. /* was limited to Pascal only in its first run.  All I have done is taken    */
  7. /* his original release, translated it to C++, and touched up a few things.  */
  8. /* I take absolutely no credit for the concepts presented in this code, and  */
  9. /* am NOT the person to ask for help if you are having trouble.              */
  10. /*                                                                           */
  11. /* Program Notes : This program presents some basic video concepts: kicking  */
  12. /*                 the computer into graphics mode, testing out two differ-  */
  13. /*                 ent methods of putting pixels to the screen, and finally  */
  14. /*                 re-entering text mode.                                    */
  15. /*                                                                           */
  16. /*                 If you are compiling this code command line, be sure to   */
  17. /*                 use the "-ml" parameter (large memory model).  Otherwise, */
  18. /*                 the program will compile and link, but will lock your     */
  19. /*                 system upon execution.                                    */
  20. /*                                                                           */
  21. /* Author        : Grant Smith (Denthor)      - smith9@batis.bis.und.ac.za   */
  22. /* Translated by : Christopher (Snowman) Mann - r3cgm@dax.cc.uakron.edu      */
  23. /*                                                                           */
  24. /* Last Modified : October 21st, 1994                                        */
  25. /*                                                                           */
  26. /*****************************************************************************/
  27.  
  28. #include <conio.h>    // getch(), clrscr()
  29. #include <dos.h>      // MK_FP, geninterrupt()
  30. #include <mem.h>      // memset()
  31. #include <stdio.h>    // printf()
  32. #include <stdlib.h>   // rand()
  33.  
  34. void SetMCGA();
  35. void SetText();
  36. void Cls(unsigned char Col);
  37. void TestINTPutpixel();
  38. void TestMEMPutpixel();
  39. void INTPutpixel(int x, int y, unsigned char Col);
  40. void MEMPutpixel(int x, int y, unsigned char Col);
  41.  
  42. // declare a pointer to the offset of VGA memory
  43. unsigned char *vga = (unsigned char *) MK_FP(0xA000, 0);
  44.  
  45. //─────────────────────────────────────────────────────────────────────────────
  46. // SetMCGA() - This function gets you into 320x200x256 mode.
  47. //─────────────────────────────────────────────────────────────────────────────
  48.  
  49. void SetMCGA() {
  50.   _AX = 0x0013;
  51.   geninterrupt (0x10);
  52. }
  53.  
  54. //─────────────────────────────────────────────────────────────────────────────
  55. // SetText() - This function gets you into text mode.
  56. //─────────────────────────────────────────────────────────────────────────────
  57.  
  58. void SetText() {
  59.   _AX = 0x0003;
  60.   geninterrupt (0x10);
  61. }
  62.  
  63. //─────────────────────────────────────────────────────────────────────────────
  64. // Cls() - This clears the screen to the specified color.
  65. //─────────────────────────────────────────────────────────────────────────────
  66.  
  67. void Cls(unsigned char Col) {
  68.   memset(vga, Col, 0xffff);
  69. }
  70.  
  71. //─────────────────────────────────────────────────────────────────────────────
  72. // INTPutpixel() - This puts a pixel on the screen using inturrupts.
  73. //─────────────────────────────────────────────────────────────────────────────
  74.  
  75. void INTPutpixel(int x, int y, unsigned char Col) {
  76.   _AH = 0x0C;
  77.   _AL = Col;
  78.   _CX = x;
  79.   _DX = y;
  80.   _BX = 0x01;
  81.   geninterrupt (0x10);
  82. }
  83.  
  84. //─────────────────────────────────────────────────────────────────────────────
  85. // TestINTPutpixel() - This tests out the speed of the INTPutpixel function.
  86. //─────────────────────────────────────────────────────────────────────────────
  87.  
  88. void TestINTPutpixel() {
  89.  
  90.   int loop1,loop2;
  91.  
  92.   for (loop1=0;loop1<320;loop1++) {
  93.     for (loop2=0;loop2<200;loop2++) {
  94.       INTPutpixel (loop1,loop2,rand());
  95.     }
  96.   }
  97.   getch();
  98.   Cls(0);
  99. }
  100.  
  101. //─────────────────────────────────────────────────────────────────────────────
  102. // MEMPutpixel() - This puts a pixel on the screen by writing directly to
  103. //                 memory.
  104. //─────────────────────────────────────────────────────────────────────────────
  105.  
  106. void MEMPutpixel (int x, int y, unsigned char Col) {
  107.   memset(vga+x+(y*320),Col,1);
  108. }
  109.  
  110. //─────────────────────────────────────────────────────────────────────────────
  111. // TestMEMPutpixel() - This tests out the speed of the MEMPutpixel function.
  112. //─────────────────────────────────────────────────────────────────────────────
  113.  
  114. void TestMEMPutpixel () {
  115.  
  116.   int loop1,loop2;
  117.  
  118.   for (loop1=0;loop1<320;loop1++) {
  119.     for (loop2=0;loop2<200;loop2++) {
  120.       MEMPutpixel (loop1,loop2,rand());
  121.     }
  122.   }
  123.   getch();
  124.   Cls(0);
  125. }
  126.  
  127. //─────────────────────────────────────────────────────────────────────────────
  128. //                                MAIN FUNCTION
  129. //─────────────────────────────────────────────────────────────────────────────
  130.  
  131. void main() {
  132.   clrscr();
  133.   printf ("What will happen is that I will clear the screen twice.  After\n");
  134.   printf ("each clear screen you will have to hit a key.  I will then fill\n");
  135.   printf ("the screen twice with randomly colored pixels using 2 different\n");
  136.   printf ("methods, after each of which you will have to hit a key. I will\n");
  137.   printf ("then return you to text mode.\n\n");
  138.   printf ("Hit any key to continue ...\n");
  139.   getch();
  140.  
  141.   SetMCGA();
  142.   Cls(32);
  143.   getch();
  144.   Cls(90);
  145.   getch();
  146.   TestINTPutpixel();
  147.   TestMEMPutpixel();
  148.   SetText();
  149.  
  150.   printf ("All done. This concludes the 1st sample program in the ASPHYXIA\n");
  151.   printf ("Training series. You may reach DENTHOR under the name of GRANT\n");
  152.   printf ("SMITH on the MailBox BBS, or leave a message to ASPHYXIA on the\n");
  153.   printf ("ASPHYXIA BBS. Get the numbers from Roblist, or write to :\n");
  154.   printf ("             Grant Smith\n");
  155.   printf ("             P.O. Box 270\n");
  156.   printf ("             Kloof\n");
  157.   printf ("             3640\n");
  158.   printf ("I hope to hear from you soon!\n\n\n");
  159.   printf ("Hit any key to exit ...");
  160.   getch();
  161. }
  162.